package tree.test199;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/17 10:56
 */
public class Solution1 {

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root != null) {
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root);
            while (!q.isEmpty()) {
                int size = q.size();
                while (size > 0) {
                    root = q.poll();
                    if (size == 1) {
                        res.add(root.val);
                    }
                    if (root.left != null) {
                        q.add(root.left);
                    }
                    if (root.right != null) {
                        q.add(root.right);
                    }
                    size--;
                }
            }
        }
        return res;
    }
}
